package sports;

import java.io.IOException;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import bean.User_record;
import DB.DB_connection_pool;
import algorithm.Basketball_credit;
import algorithm.Point_record;

public class Basketball_handle extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		JSONObject jsonObj = new JSONObject();
		String user_account = request.getSession().getAttribute("user_account").toString();
		
		int basketball_point = Integer.parseInt(request.getParameter("basketball_point"));   //得分
		int basketball_steal = Integer.parseInt(request.getParameter("basketball_steal"));    //抢断
		int basketball_rebound = Integer.parseInt(request.getParameter("basketball_rebound"));   //篮板
		int basketball_assist = Integer.parseInt(request.getParameter("basketball_assist"));    //助攻
		int basketball_level = Integer.parseInt(request.getParameter("basketball_level"));    //比赛等级
		
		double basketball_record = User_record.getBasketball_record(user_account);    //历史记录
		
		try {
			Connection conn = DB_connection_pool.data_pool.getConnection();
		
			Basketball_credit basketball_credit = new Basketball_credit();
			Map<String,Double> map = basketball_credit.Get_basketball_score(basketball_point, basketball_steal, basketball_rebound, basketball_assist, basketball_level, basketball_record);
			
			Point_record point_record = new Point_record();
			point_record.sports_record(user_account, map.get("basketball_record"), Point_record.basketball_event);   //改变篮球历史记录
			if(map.get("flag") == 1){ //积分改变
				point_record.point_record(user_account, Point_record.basketball_event, map.get("basketball_credit"), true);   //记录
				point_record.change_point(user_account, map.get("basketball_credit"));
			}else{    
				//do not
			}
			
			//插入数据
			String insert_sql = "insert into basketball_patemeter(basketball_point , basketball_steal , basketball_rebound , basketball_assist , basketball_level , user_account) values(?,?,?,?,?,?)";
			PreparedStatement ps_insert = conn.prepareStatement(insert_sql);
			ps_insert.setInt(1, basketball_point);
			ps_insert.setInt(2, basketball_steal);
			ps_insert.setInt(3, basketball_rebound);
			ps_insert.setInt(4, basketball_assist);
			ps_insert.setInt(5, basketball_level);
			ps_insert.setString(6, user_account);
			ps_insert.executeUpdate();
			ps_insert.close();
			
			
			conn.close();
			jsonObj.put("result", true);
			
		} catch (SQLException e) {
			jsonObj.put("result", false);
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
      out.print(jsonObj.toString());
		
	}

}
